spa.nav.login = (function($){
    var
        configMap = {
            main_html : String() +
                '<ul class="nav navbar-nav navbar-right">'+
                    '<li class="dropdown role">'+
                        '<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="role-title">我是... </span><b class="caret"></b></a>' +
                        '<ul class="dropdown-menu roles">' +

                        '</ul>' +
                    '</li>' +
                    '<li class="dropdown object">' +
                        '<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="object-title">管理... </span><b class="caret"></b></a>' +
                        '<ul class="dropdown-menu objects">' +

                        '</ul>' +
                    '</li>' +
                    '<li>' +
                        '<a href="#" class="user-login">' +
                            '<span class="visible-sm visible-xs">' +
                                '登录' +
                                '<span class="fui-user"></span>' +
                            '</span>' +
                            '<span class="visible-md visible-lg">' +
                                '<span class="fui-user"></span>' +
                            '</span>' +
                        '</a>' +
                    '</li>' +
                '</ul>',
            login_fields : [
                spa.fields.CharField({label:'用户名称',name:'username'}),
                spa.fields.CharField({label:'用户密码',name:'password'})
            ],
            login_title : '登录系统'
      },
        stateMap = {
            $container : null
        },
        jqueryMap = {

        },initModule,configModule,setJqueryMap

    setJqueryMap = function(){
        var $container = stateMap.$container,
            $right = $(configMap.main_html),
            $role_title = $right.find('.role-title'),
            $roles = $right.find('ul.roles'),
            $object_title = $right.find('.object-title'),
            $objects = $right.find('ul.objects'),
            $user_login = $right.find(".user-login")
        $container.append($right)
        jqueryMap = {
            $container : $container,
            $right : $right,
            $role_title  : $role_title,
            $roles : $roles,
            $object_title : $object_title,
            $objects : $objects,
            $user_login : $user_login
        }

        // bind event
        $user_login.click(function(){
            spa.modal.setTitle(configMap.login_title)
            spa.modal.setFields(configMap.login_fields)
            spa.modal.setCallback(function($modal){
                var
                    options = {
                        url : '/accounts/login/',
                        data : {
                            username : configMap.login_fields[0].getValue(),
                            password : configMap.login_fields[1].getValue()
                        }
                    }

                    spa.model.User.login(options)
                $modal.modal("hide")
            })
            spa.modal.show()
        })

        $.gevent.subscribe(jqueryMap.$roles,spa.events.spaEventLogin,function(){
            var db = spa.model.User.getDb(),index,
                roles = db().get(),current_role_id,current_object_id
            $roles.html('')
            for(index in roles){
                $roles.append('<li><a href="'+roles[index].id+'">'+roles[index].name+'</a></li>')
            }
            $roles.find("li a").click(function(event){
                var $this = $(this),role_id = $this.attr("href"),role,index,objects

                $this.parents('li').trigger('click')
                if(current_role_id===role_id){
                    return false
                }
                current_role_id = role_id
                jqueryMap.$object_title.text('管理...')
                jqueryMap.$objects.html('')
                role = db().get({id:$this.attr("href")})
                jqueryMap.$role_title.text('我是[' + $this.text() + ']')
                if(role) {
                    objects = role[0].objects
                    for (index in objects) {
                        jqueryMap.$objects.append('<li><a href="' + objects[index].id + '">' + objects[index].text + '</a></li>')
               }
                    jqueryMap.$objects.find("li a").click(function(event){
                        var $this = $(this),object_id=$this.attr('href')

                        $this.parents("li").trigger('click')
                        if (current_object_id===object_id){
                            return false
                        }
                        current_object_id = object_id
                        jqueryMap.$object_title.text('管理[' + $this.text() + ']')
                        spa.model.Grid.getGrid({url:'/base/grid/'+current_role_id+'/'+current_object_id+'/'})
                        return false
                    })
                }
                return false
            })
            $.gevent.unsubscribe(jqueryMap.$roles,spa.events.spaEventLogin)
        })
    }

    configModule = function(){

    }

    initModule = function($container){
        stateMap.$container = $container
        setJqueryMap()
    }

    return {
        initModule : initModule
    }
}(jQuery))